{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Task: Word Count </h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Count the number of times each word occurs\n",
    "<br>\n",
    "\n",
    "For the text below, \n",
    "<br>\n",
    "a) Clean punctuation and transform all words to lower case.\n",
    "<br>\n",
    "b) Output a list of word count pairs sorted from highest to lowest count."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Text=\"\"\"\n",
    "bands which have connected them with another, and to assume among the\n",
    "powers of the earth, the separate and equal station to which the Laws\n",
    "of Nature and of Nature's God entitle them, a decent respect to the\n",
    "opinions of mankind requires that they should declare the causes which\n",
    "impel them to the separation.  We hold these truths to be\n",
    "self-evident, that all men are created equal, that they are endowed by\n",
    "their Creator with certain unalienable Rights, that among these are\n",
    "Life, Liberty and the pursuit of Happiness.--That to secure these\n",
    "rights, Governments are instituted among Men, deriving their just\n",
    "powers from the consent of the governed, --That whenever any Form of\n",
    "Government becomes destructive of these ends, it is the Right of the\n",
    "People to alter or to abolish it, and to institute new Government,\n",
    "laying its foundation on such principles and organizing its powers in\n",
    "such form, as to them shall seem most likely to effect their Safety\n",
    "and Happiness. Prudence, indeed, will dictate that Governments long\n",
    "established should not be changed for light and transient causes; and\n",
    "accordingly all experience hath shewn, that mankind are more disposed\n",
    "to suffer, while evils are sufferable, than to right themselves by\n",
    "abolishing the forms to which they are accustomed. But when a long\n",
    "train of abuses and usurpations, pursuing invariably the same Object\n",
    "evinces a design to reduce them under absolute Despotism, it is their\n",
    "right, it is their duty, to throw off such Government, and to provide\n",
    "new Guards for their future security.--Such has been the patient\n",
    "sufferance of these Colonies; and such is now the necessity which\n",
    "constrains them to alter their former Systems of Government. The\n",
    "history of the present King of Great Britain is a history of repeated\n",
    "injuries and usurpations, all having in direct object the\n",
    "establishment of an absolute Tyranny over these States. To prove this,\n",
    "let Facts be submitted to a candid world.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Cleaning text and lower casing all words\n",
    "for char in '-.,\\n':\n",
    "    Text=Text.replace(char,' ')\n",
    "Text = Text.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " bands which have connected them with another  and to assume among the powers of the earth  the separate and equal station to which the laws of nature and of nature's god entitle them  a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation   we hold these truths to be self evident  that all men are created equal  that they are endowed by their creator with certain unalienable rights  that among these are life  liberty and the pursuit of happiness   that to secure these rights  governments are instituted among men  deriving their just powers from the consent of the governed    that whenever any form of government becomes destructive of these ends  it is the right of the people to alter or to abolish it  and to institute new government  laying its foundation on such principles and organizing its powers in such form  as to them shall seem most likely to effect their safety and happiness  prudence  indeed  will dictate that governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn  that mankind are more disposed to suffer  while evils are sufferable  than to right themselves by abolishing the forms to which they are accustomed  but when a long train of abuses and usurpations  pursuing invariably the same object evinces a design to reduce them under absolute despotism  it is their right  it is their duty  to throw off such government  and to provide new guards for their future security   such has been the patient sufferance of these colonies; and such is now the necessity which constrains them to alter their former systems of government  the history of the present king of great britain is a history of repeated injuries and usurpations  all having in direct object the establishment of an absolute tyranny over these states  to prove this  let facts be submitted to a candid world  \n"
     ]
    }
   ],
   "source": [
    "print(Text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bands', 'which', 'have', 'connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'powers', 'of', 'the', 'earth', 'the', 'separate', 'and', 'equal', 'station', 'to', 'which', 'the', 'laws', 'of', 'nature', 'and', 'of', \"nature's\", 'god', 'entitle', 'them', 'a', 'decent', 'respect', 'to', 'the', 'opinions', 'of', 'mankind', 'requires', 'that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to', 'the', 'separation', 'we', 'hold', 'these', 'truths', 'to', 'be', 'self', 'evident', 'that', 'all', 'men', 'are', 'created', 'equal', 'that', 'they', 'are', 'endowed', 'by', 'their', 'creator', 'with', 'certain', 'unalienable', 'rights', 'that', 'among', 'these', 'are', 'life', 'liberty', 'and', 'the', 'pursuit', 'of', 'happiness', 'that', 'to', 'secure', 'these', 'rights', 'governments', 'are', 'instituted', 'among', 'men', 'deriving', 'their', 'just', 'powers', 'from', 'the', 'consent', 'of', 'the', 'governed', 'that', 'whenever', 'any', 'form', 'of', 'government', 'becomes', 'destructive', 'of', 'these', 'ends', 'it', 'is', 'the', 'right', 'of', 'the', 'people', 'to', 'alter', 'or', 'to', 'abolish', 'it', 'and', 'to', 'institute', 'new', 'government', 'laying', 'its', 'foundation', 'on', 'such', 'principles', 'and', 'organizing', 'its', 'powers', 'in', 'such', 'form', 'as', 'to', 'them', 'shall', 'seem', 'most', 'likely', 'to', 'effect', 'their', 'safety', 'and', 'happiness', 'prudence', 'indeed', 'will', 'dictate', 'that', 'governments', 'long', 'established', 'should', 'not', 'be', 'changed', 'for', 'light', 'and', 'transient', 'causes;', 'and', 'accordingly', 'all', 'experience', 'hath', 'shewn', 'that', 'mankind', 'are', 'more', 'disposed', 'to', 'suffer', 'while', 'evils', 'are', 'sufferable', 'than', 'to', 'right', 'themselves', 'by', 'abolishing', 'the', 'forms', 'to', 'which', 'they', 'are', 'accustomed', 'but', 'when', 'a', 'long', 'train', 'of', 'abuses', 'and', 'usurpations', 'pursuing', 'invariably', 'the', 'same', 'object', 'evinces', 'a', 'design', 'to', 'reduce', 'them', 'under', 'absolute', 'despotism', 'it', 'is', 'their', 'right', 'it', 'is', 'their', 'duty', 'to', 'throw', 'off', 'such', 'government', 'and', 'to', 'provide', 'new', 'guards', 'for', 'their', 'future', 'security', 'such', 'has', 'been', 'the', 'patient', 'sufferance', 'of', 'these', 'colonies;', 'and', 'such', 'is', 'now', 'the', 'necessity', 'which', 'constrains', 'them', 'to', 'alter', 'their', 'former', 'systems', 'of', 'government', 'the', 'history', 'of', 'the', 'present', 'king', 'of', 'great', 'britain', 'is', 'a', 'history', 'of', 'repeated', 'injuries', 'and', 'usurpations', 'all', 'having', 'in', 'direct', 'object', 'the', 'establishment', 'of', 'an', 'absolute', 'tyranny', 'over', 'these', 'states', 'to', 'prove', 'this', 'let', 'facts', 'be', 'submitted', 'to', 'a', 'candid', 'world']\n"
     ]
    }
   ],
   "source": [
    "# split returns a list of words delimited by sequences of whitespace (including tabs, newlines, etc, like re's \\s) \n",
    "word_list = Text.split()\n",
    "print(word_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Reading in tab delimited text file\n",
    "f = open(\"word_list.txt\",\"r\")\n",
    "filedata = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bands\\twhich\\thave\\tconnected\\tthem\\twith\\tanother\\tand\\t'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filedata[0:49]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "word_list = filedata.split(\"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bands', 'which', 'have', 'connected', 'them']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_list[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Initializing Dictionary\n",
    "d = {}\n",
    "\n",
    "# counting number of times each word comes up in list of words (in dictionary)\n",
    "for word in word_list: \n",
    "    d[word] = d.get(word, 0) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 5,\n",
       " 'abolish': 1,\n",
       " 'abolishing': 1,\n",
       " 'absolute': 2,\n",
       " 'abuses': 1,\n",
       " 'accordingly': 1,\n",
       " 'accustomed': 1,\n",
       " 'all': 3,\n",
       " 'alter': 2,\n",
       " 'among': 3,\n",
       " 'an': 1,\n",
       " 'and': 13,\n",
       " 'another': 1,\n",
       " 'any': 1,\n",
       " 'are': 7,\n",
       " 'as': 1,\n",
       " 'assume': 1,\n",
       " 'bands': 1,\n",
       " 'be': 3,\n",
       " 'becomes': 1,\n",
       " 'been': 1,\n",
       " 'britain': 1,\n",
       " 'but': 1,\n",
       " 'by': 2,\n",
       " 'candid': 1,\n",
       " 'causes': 1,\n",
       " 'causes;': 1,\n",
       " 'certain': 1,\n",
       " 'changed': 1,\n",
       " 'colonies;': 1,\n",
       " 'connected': 1,\n",
       " 'consent': 1,\n",
       " 'constrains': 1,\n",
       " 'created': 1,\n",
       " 'creator': 1,\n",
       " 'decent': 1,\n",
       " 'declare': 1,\n",
       " 'deriving': 1,\n",
       " 'design': 1,\n",
       " 'despotism': 1,\n",
       " 'destructive': 1,\n",
       " 'dictate': 1,\n",
       " 'direct': 1,\n",
       " 'disposed': 1,\n",
       " 'duty': 1,\n",
       " 'earth': 1,\n",
       " 'effect': 1,\n",
       " 'endowed': 1,\n",
       " 'ends': 1,\n",
       " 'entitle': 1,\n",
       " 'equal': 2,\n",
       " 'established': 1,\n",
       " 'establishment': 1,\n",
       " 'evident': 1,\n",
       " 'evils': 1,\n",
       " 'evinces': 1,\n",
       " 'experience': 1,\n",
       " 'facts': 1,\n",
       " 'for': 2,\n",
       " 'form': 2,\n",
       " 'former': 1,\n",
       " 'forms': 1,\n",
       " 'foundation': 1,\n",
       " 'from': 1,\n",
       " 'future': 1,\n",
       " 'god': 1,\n",
       " 'governed': 1,\n",
       " 'government': 4,\n",
       " 'governments': 2,\n",
       " 'great': 1,\n",
       " 'guards': 1,\n",
       " 'happiness': 2,\n",
       " 'has': 1,\n",
       " 'hath': 1,\n",
       " 'have': 1,\n",
       " 'having': 1,\n",
       " 'history': 2,\n",
       " 'hold': 1,\n",
       " 'impel': 1,\n",
       " 'in': 2,\n",
       " 'indeed': 1,\n",
       " 'injuries': 1,\n",
       " 'institute': 1,\n",
       " 'instituted': 1,\n",
       " 'invariably': 1,\n",
       " 'is': 5,\n",
       " 'it': 4,\n",
       " 'its': 2,\n",
       " 'just': 1,\n",
       " 'king': 1,\n",
       " 'laws': 1,\n",
       " 'laying': 1,\n",
       " 'let': 1,\n",
       " 'liberty': 1,\n",
       " 'life': 1,\n",
       " 'light': 1,\n",
       " 'likely': 1,\n",
       " 'long': 2,\n",
       " 'mankind': 2,\n",
       " 'men': 2,\n",
       " 'more': 1,\n",
       " 'most': 1,\n",
       " 'nature': 1,\n",
       " \"nature's\": 1,\n",
       " 'necessity': 1,\n",
       " 'new': 2,\n",
       " 'not': 1,\n",
       " 'now': 1,\n",
       " 'object': 2,\n",
       " 'of': 16,\n",
       " 'off': 1,\n",
       " 'on': 1,\n",
       " 'opinions': 1,\n",
       " 'or': 1,\n",
       " 'organizing': 1,\n",
       " 'over': 1,\n",
       " 'patient': 1,\n",
       " 'people': 1,\n",
       " 'powers': 3,\n",
       " 'present': 1,\n",
       " 'principles': 1,\n",
       " 'prove': 1,\n",
       " 'provide': 1,\n",
       " 'prudence': 1,\n",
       " 'pursuing': 1,\n",
       " 'pursuit': 1,\n",
       " 'reduce': 1,\n",
       " 'repeated': 1,\n",
       " 'requires': 1,\n",
       " 'respect': 1,\n",
       " 'right': 3,\n",
       " 'rights': 2,\n",
       " 'safety': 1,\n",
       " 'same': 1,\n",
       " 'secure': 1,\n",
       " 'security': 1,\n",
       " 'seem': 1,\n",
       " 'self': 1,\n",
       " 'separate': 1,\n",
       " 'separation': 1,\n",
       " 'shall': 1,\n",
       " 'shewn': 1,\n",
       " 'should': 2,\n",
       " 'states': 1,\n",
       " 'station': 1,\n",
       " 'submitted': 1,\n",
       " 'such': 5,\n",
       " 'suffer': 1,\n",
       " 'sufferable': 1,\n",
       " 'sufferance': 1,\n",
       " 'systems': 1,\n",
       " 'than': 1,\n",
       " 'that': 8,\n",
       " 'the': 19,\n",
       " 'their': 7,\n",
       " 'them': 6,\n",
       " 'themselves': 1,\n",
       " 'these': 6,\n",
       " 'they': 3,\n",
       " 'this': 1,\n",
       " 'throw': 1,\n",
       " 'to': 20,\n",
       " 'train': 1,\n",
       " 'transient': 1,\n",
       " 'truths': 1,\n",
       " 'tyranny': 1,\n",
       " 'unalienable': 1,\n",
       " 'under': 1,\n",
       " 'usurpations': 2,\n",
       " 'we': 1,\n",
       " 'when': 1,\n",
       " 'whenever': 1,\n",
       " 'which': 5,\n",
       " 'while': 1,\n",
       " 'will': 1,\n",
       " 'with': 2,\n",
       " 'world': 1}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 'opinions'),\n",
       " (1, 'all'),\n",
       " (1, 'just'),\n",
       " (1, 'consent'),\n",
       " (1, 'earth'),\n",
       " (2, 'its'),\n",
       " (1, 'causes'),\n",
       " (1, 'should'),\n",
       " (11, 'to'),\n",
       " (1, 'alter'),\n",
       " (4, 'them'),\n",
       " (2, 'government'),\n",
       " (1, 'bands'),\n",
       " (2, 'they'),\n",
       " (1, 'governments'),\n",
       " (1, 'laying'),\n",
       " (1, 'right'),\n",
       " (1, 'people'),\n",
       " (1, 'truths'),\n",
       " (1, 'ends'),\n",
       " (1, 'likely'),\n",
       " (4, 'are'),\n",
       " (1, \"nature's\"),\n",
       " (1, 'organizing'),\n",
       " (1, 'principles'),\n",
       " (1, 'god'),\n",
       " (1, 'shall'),\n",
       " (1, 'liberty'),\n",
       " (1, 'unalienable'),\n",
       " (1, 'safety'),\n",
       " (2, 'happiness'),\n",
       " (1, 'new'),\n",
       " (1, 'foundation'),\n",
       " (1, 'we'),\n",
       " (1, 'nature'),\n",
       " (2, 'men'),\n",
       " (1, 'pursuit'),\n",
       " (1, 'separation'),\n",
       " (1, 'by'),\n",
       " (1, 'on'),\n",
       " (1, 'created'),\n",
       " (1, 'institute'),\n",
       " (9, 'of'),\n",
       " (2, 'equal'),\n",
       " (1, 'or'),\n",
       " (3, 'among'),\n",
       " (1, 'secure'),\n",
       " (1, 'another'),\n",
       " (1, 'respect'),\n",
       " (1, 'from'),\n",
       " (1, 'decent'),\n",
       " (1, 'creator'),\n",
       " (3, 'their'),\n",
       " (1, 'station'),\n",
       " (1, 'entitle'),\n",
       " (1, 'certain'),\n",
       " (1, 'life'),\n",
       " (2, 'form'),\n",
       " (6, 'that'),\n",
       " (1, 'becomes'),\n",
       " (1, 'instituted'),\n",
       " (1, 'be'),\n",
       " (1, 'hold'),\n",
       " (2, 'with'),\n",
       " (1, 'evident'),\n",
       " (2, 'rights'),\n",
       " (4, 'these'),\n",
       " (1, 'impel'),\n",
       " (1, 'assume'),\n",
       " (3, 'powers'),\n",
       " (1, 'declare'),\n",
       " (7, 'and'),\n",
       " (1, 'endowed'),\n",
       " (1, 'is'),\n",
       " (2, 'it'),\n",
       " (1, 'as'),\n",
       " (1, 'have'),\n",
       " (1, 'in'),\n",
       " (1, 'seem'),\n",
       " (1, 'any'),\n",
       " (1, 'self'),\n",
       " (1, 'abolish'),\n",
       " (3, 'which'),\n",
       " (1, 'separate'),\n",
       " (1, 'effect'),\n",
       " (1, 'deriving'),\n",
       " (1, 'most'),\n",
       " (1, 'connected'),\n",
       " (1, 'mankind'),\n",
       " (2, 'such'),\n",
       " (1, 'destructive'),\n",
       " (1, 'a'),\n",
       " (1, 'whenever'),\n",
       " (1, 'governed'),\n",
       " (12, 'the'),\n",
       " (1, 'requires'),\n",
       " (1, 'laws')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Reverses the key and values so they can be sorted\n",
    "# using tuples\n",
    "word_freq = []\n",
    "for key, value in d.items():\n",
    "    word_freq.append((value, key))\n",
    "word_freq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(12, 'the'), (11, 'to'), (9, 'of'), (7, 'and'), (6, 'that'), (4, 'these'), (4, 'them'), (4, 'are'), (3, 'which'), (3, 'their'), (3, 'powers'), (3, 'among'), (2, 'with'), (2, 'they'), (2, 'such'), (2, 'rights'), (2, 'men'), (2, 'its'), (2, 'it'), (2, 'happiness'), (2, 'government'), (2, 'form'), (2, 'equal'), (1, 'whenever'), (1, 'we'), (1, 'unalienable'), (1, 'truths'), (1, 'station'), (1, 'should'), (1, 'shall'), (1, 'separation'), (1, 'separate'), (1, 'self'), (1, 'seem'), (1, 'secure'), (1, 'safety'), (1, 'right'), (1, 'respect'), (1, 'requires'), (1, 'pursuit'), (1, 'principles'), (1, 'people'), (1, 'organizing'), (1, 'or'), (1, 'opinions'), (1, 'on'), (1, 'new'), (1, \"nature's\"), (1, 'nature'), (1, 'most'), (1, 'mankind'), (1, 'likely'), (1, 'life'), (1, 'liberty'), (1, 'laying'), (1, 'laws'), (1, 'just'), (1, 'is'), (1, 'instituted'), (1, 'institute'), (1, 'in'), (1, 'impel'), (1, 'hold'), (1, 'have'), (1, 'governments'), (1, 'governed'), (1, 'god'), (1, 'from'), (1, 'foundation'), (1, 'evident'), (1, 'entitle'), (1, 'ends'), (1, 'endowed'), (1, 'effect'), (1, 'earth'), (1, 'destructive'), (1, 'deriving'), (1, 'declare'), (1, 'decent'), (1, 'creator'), (1, 'created'), (1, 'consent'), (1, 'connected'), (1, 'certain'), (1, 'causes'), (1, 'by'), (1, 'becomes'), (1, 'be'), (1, 'bands'), (1, 'assume'), (1, 'as'), (1, 'any'), (1, 'another'), (1, 'alter'), (1, 'all'), (1, 'abolish'), (1, 'a')]\n"
     ]
    }
   ],
   "source": [
    "word_freq.sort(reverse=True)\n",
    "print(word_freq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 2. Not using Get Method\n",
    "\n",
    "# Initializing Dictionary\n",
    "d = {}\n",
    "\n",
    "# counting number of times each word comes up in list of words (in dictionary)\n",
    "for word in word_list:\n",
    "    if word not in d:\n",
    "        d[word] = 0\n",
    "    d[word] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1,\n",
       " 'abolish': 1,\n",
       " 'all': 1,\n",
       " 'alter': 1,\n",
       " 'among': 3,\n",
       " 'and': 7,\n",
       " 'another': 1,\n",
       " 'any': 1,\n",
       " 'are': 4,\n",
       " 'as': 1,\n",
       " 'assume': 1,\n",
       " 'bands': 1,\n",
       " 'be': 1,\n",
       " 'becomes': 1,\n",
       " 'by': 1,\n",
       " 'causes': 1,\n",
       " 'certain': 1,\n",
       " 'connected': 1,\n",
       " 'consent': 1,\n",
       " 'created': 1,\n",
       " 'creator': 1,\n",
       " 'decent': 1,\n",
       " 'declare': 1,\n",
       " 'deriving': 1,\n",
       " 'destructive': 1,\n",
       " 'earth': 1,\n",
       " 'effect': 1,\n",
       " 'endowed': 1,\n",
       " 'ends': 1,\n",
       " 'entitle': 1,\n",
       " 'equal': 2,\n",
       " 'evident': 1,\n",
       " 'form': 2,\n",
       " 'foundation': 1,\n",
       " 'from': 1,\n",
       " 'god': 1,\n",
       " 'governed': 1,\n",
       " 'government': 2,\n",
       " 'governments': 1,\n",
       " 'happiness': 2,\n",
       " 'have': 1,\n",
       " 'hold': 1,\n",
       " 'impel': 1,\n",
       " 'in': 1,\n",
       " 'institute': 1,\n",
       " 'instituted': 1,\n",
       " 'is': 1,\n",
       " 'it': 2,\n",
       " 'its': 2,\n",
       " 'just': 1,\n",
       " 'laws': 1,\n",
       " 'laying': 1,\n",
       " 'liberty': 1,\n",
       " 'life': 1,\n",
       " 'likely': 1,\n",
       " 'mankind': 1,\n",
       " 'men': 2,\n",
       " 'most': 1,\n",
       " 'nature': 1,\n",
       " \"nature's\": 1,\n",
       " 'new': 1,\n",
       " 'of': 9,\n",
       " 'on': 1,\n",
       " 'opinions': 1,\n",
       " 'or': 1,\n",
       " 'organizing': 1,\n",
       " 'people': 1,\n",
       " 'powers': 3,\n",
       " 'principles': 1,\n",
       " 'pursuit': 1,\n",
       " 'requires': 1,\n",
       " 'respect': 1,\n",
       " 'right': 1,\n",
       " 'rights': 2,\n",
       " 'safety': 1,\n",
       " 'secure': 1,\n",
       " 'seem': 1,\n",
       " 'self': 1,\n",
       " 'separate': 1,\n",
       " 'separation': 1,\n",
       " 'shall': 1,\n",
       " 'should': 1,\n",
       " 'station': 1,\n",
       " 'such': 2,\n",
       " 'that': 6,\n",
       " 'the': 12,\n",
       " 'their': 3,\n",
       " 'them': 4,\n",
       " 'these': 4,\n",
       " 'they': 2,\n",
       " 'to': 11,\n",
       " 'truths': 1,\n",
       " 'unalienable': 1,\n",
       " 'we': 1,\n",
       " 'whenever': 1,\n",
       " 'which': 3,\n",
       " 'with': 2}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(12, 'the'), (11, 'to'), (9, 'of'), (7, 'and'), (6, 'that'), (4, 'these'), (4, 'them'), (4, 'are'), (3, 'which'), (3, 'their'), (3, 'powers'), (3, 'among'), (2, 'with'), (2, 'they'), (2, 'such'), (2, 'rights'), (2, 'men'), (2, 'its'), (2, 'it'), (2, 'happiness'), (2, 'government'), (2, 'form'), (2, 'equal'), (1, 'whenever'), (1, 'we'), (1, 'unalienable'), (1, 'truths'), (1, 'station'), (1, 'should'), (1, 'shall'), (1, 'separation'), (1, 'separate'), (1, 'self'), (1, 'seem'), (1, 'secure'), (1, 'safety'), (1, 'right'), (1, 'respect'), (1, 'requires'), (1, 'pursuit'), (1, 'principles'), (1, 'people'), (1, 'organizing'), (1, 'or'), (1, 'opinions'), (1, 'on'), (1, 'new'), (1, \"nature's\"), (1, 'nature'), (1, 'most'), (1, 'mankind'), (1, 'likely'), (1, 'life'), (1, 'liberty'), (1, 'laying'), (1, 'laws'), (1, 'just'), (1, 'is'), (1, 'instituted'), (1, 'institute'), (1, 'in'), (1, 'impel'), (1, 'hold'), (1, 'have'), (1, 'governments'), (1, 'governed'), (1, 'god'), (1, 'from'), (1, 'foundation'), (1, 'evident'), (1, 'entitle'), (1, 'ends'), (1, 'endowed'), (1, 'effect'), (1, 'earth'), (1, 'destructive'), (1, 'deriving'), (1, 'declare'), (1, 'decent'), (1, 'creator'), (1, 'created'), (1, 'consent'), (1, 'connected'), (1, 'certain'), (1, 'causes'), (1, 'by'), (1, 'becomes'), (1, 'be'), (1, 'bands'), (1, 'assume'), (1, 'as'), (1, 'any'), (1, 'another'), (1, 'alter'), (1, 'all'), (1, 'abolish'), (1, 'a')]\n"
     ]
    }
   ],
   "source": [
    "# this next bit of code reverses the key and values so they can be sorted\n",
    "# using tuples\n",
    "word_freq = []\n",
    "for key, value in d.items():\n",
    "    word_freq.append((value, key))\n",
    "word_freq.sort(reverse=True)\n",
    "print word_freq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('the', 12),\n",
       " ('to', 11),\n",
       " ('of', 9),\n",
       " ('and', 7),\n",
       " ('that', 6),\n",
       " ('them', 4),\n",
       " ('are', 4),\n",
       " ('these', 4),\n",
       " ('among', 3),\n",
       " ('their', 3),\n",
       " ('powers', 3),\n",
       " ('which', 3),\n",
       " ('its', 2),\n",
       " ('government', 2),\n",
       " ('they', 2),\n",
       " ('happiness', 2),\n",
       " ('men', 2),\n",
       " ('equal', 2),\n",
       " ('form', 2),\n",
       " ('with', 2),\n",
       " ('rights', 2),\n",
       " ('it', 2),\n",
       " ('such', 2),\n",
       " ('opinions', 1),\n",
       " ('all', 1),\n",
       " ('just', 1),\n",
       " ('consent', 1),\n",
       " ('earth', 1),\n",
       " ('causes', 1),\n",
       " ('should', 1),\n",
       " ('alter', 1),\n",
       " ('bands', 1),\n",
       " ('governments', 1),\n",
       " ('laying', 1),\n",
       " ('right', 1),\n",
       " ('people', 1),\n",
       " ('truths', 1),\n",
       " ('ends', 1),\n",
       " ('likely', 1),\n",
       " (\"nature's\", 1),\n",
       " ('organizing', 1),\n",
       " ('principles', 1),\n",
       " ('god', 1),\n",
       " ('shall', 1),\n",
       " ('liberty', 1),\n",
       " ('unalienable', 1),\n",
       " ('safety', 1),\n",
       " ('new', 1),\n",
       " ('foundation', 1),\n",
       " ('we', 1),\n",
       " ('nature', 1),\n",
       " ('pursuit', 1),\n",
       " ('separation', 1),\n",
       " ('by', 1),\n",
       " ('on', 1),\n",
       " ('created', 1),\n",
       " ('institute', 1),\n",
       " ('or', 1),\n",
       " ('secure', 1),\n",
       " ('another', 1),\n",
       " ('respect', 1),\n",
       " ('from', 1),\n",
       " ('decent', 1),\n",
       " ('creator', 1),\n",
       " ('station', 1),\n",
       " ('entitle', 1),\n",
       " ('certain', 1),\n",
       " ('life', 1),\n",
       " ('becomes', 1),\n",
       " ('instituted', 1),\n",
       " ('be', 1),\n",
       " ('hold', 1),\n",
       " ('evident', 1),\n",
       " ('impel', 1),\n",
       " ('assume', 1),\n",
       " ('declare', 1),\n",
       " ('endowed', 1),\n",
       " ('is', 1),\n",
       " ('as', 1),\n",
       " ('have', 1),\n",
       " ('in', 1),\n",
       " ('seem', 1),\n",
       " ('any', 1),\n",
       " ('self', 1),\n",
       " ('abolish', 1),\n",
       " ('separate', 1),\n",
       " ('effect', 1),\n",
       " ('deriving', 1),\n",
       " ('most', 1),\n",
       " ('connected', 1),\n",
       " ('mankind', 1),\n",
       " ('destructive', 1),\n",
       " ('a', 1),\n",
       " ('whenever', 1),\n",
       " ('governed', 1),\n",
       " ('requires', 1),\n",
       " ('laws', 1)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. Solution using inbuilt methods\n",
    "# Probably not practical to expect for new programmers to Python\n",
    "\n",
    "# initializing a dictionary\n",
    "d = {};\n",
    "\n",
    "# counting number of times each word comes up in list of words\n",
    "for key in word_list: \n",
    "    d[key] = d.get(key, 0) + 1\n",
    "\n",
    "sorted(d.items(), key = lambda x: x[1], reverse = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.youtube.com/watch?v=l_dIleafLZ8"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
